<template>
  <div class="instructions-container">
    <!-- 页面头部 -->
    <div class="instructions-header">
      <div class="header-content">
        <div class="header-title">
          <i class="el-icon-document-copy"></i>
          <h2>操作指南管理</h2>
        </div>
        <p class="header-description">管理系统操作指南，支持添加、编辑、删除操作指南</p>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="instructions-main">
      <el-card class="action-card" shadow="hover">
        <div slot="header" class="card-header">
          <div class="card-title">
            <i class="el-icon-list"></i>
            <span>操作指南列表</span>
          </div>
          <el-button 
            type="primary" 
            size="small" 
            @click="showAddInstructionDialog"
            icon="el-icon-plus"
          >
            新增指南
          </el-button>
        </div>
        
        <div v-loading="loading" class="instructions-list">
          <div v-if="instructionsList.length === 0 && !loading" class="empty-list">
            <div class="empty-icon">
              <i class="el-icon-document"></i>
            </div>
            <p class="empty-text">暂无操作指南</p>
            <p class="empty-hint">点击"新增指南"按钮创建第一个操作指南</p>
          </div>
          <div v-else class="instructions-grid">
            <div 
              v-for="item in instructionsList" 
              :key="item.id" 
              class="instruction-card"
            >
              <div class="card-image">
                <img 
                  v-if="item.img" 
                  :src="item.img" 
                  :alt="item.title"
                  @error="handleImageError"
                />
                <div v-else class="no-image">
                  <i class="el-icon-picture"></i>
                </div>
                <div class="image-overlay">
                  <el-button 
                    type="primary" 
                    size="mini" 
                    icon="el-icon-view"
                    @click="viewInstructionDetail(item)"
                    class="overlay-btn"
                  >
                    查看
                  </el-button>
                </div>
              </div>
              <div class="card-content">
                <h3 class="card-title">{{ item.title }}</h3>
                <p class="card-desc">{{ item.content.substring(0, 80) }}...</p>
                <div class="card-meta">
                  <span class="create-time">
                    <i class="el-icon-time"></i>
                    {{ formatDate(item.createdAt) }}
                  </span>
                </div>
              </div>
              <div class="card-actions">
                <el-button 
                  type="text" 
                  size="small" 
                  @click="viewInstructionDetail(item)"
                  icon="el-icon-view"
                >
                  查看
                </el-button>
                <el-button 
                  type="text" 
                  size="small" 
                  @click="editInstruction(item)"
                  icon="el-icon-edit"
                >
                  编辑
                </el-button>
                <el-button 
                  type="text" 
                  size="small" 
                  class="danger-text"
                  @click="deleteInstruction(item)"
                  icon="el-icon-delete"
                >
                  删除
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="instructionDialogTitle"
      :visible.sync="instructionDialogVisible"
      width="900px"
      :close-on-click-modal="false"
      @close="resetInstructionForm"
      class="instruction-dialog"
    >
      <el-form 
        ref="instructionForm" 
        :model="instructionForm" 
        :rules="instructionRules" 
        label-width="100px"
        class="instruction-form"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input 
                v-model="instructionForm.title" 
                placeholder="请输入指南标题"
                maxlength="100"
                show-word-limit
                prefix-icon="el-icon-edit"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="封面图片">
              <el-upload
                ref="imageUpload"
                action="#"
                :before-upload="beforeImageUpload"
                :on-change="handleImageChange"
                :show-file-list="false"
                accept="image/*"
                class="image-uploader"
              >
                <div v-if="instructionForm.imageUrl" class="image-preview">
                  <img :src="instructionForm.imageUrl" alt="封面图片" />
                  <div class="image-overlay">
                    <i class="el-icon-edit"></i>
                    <i class="el-icon-delete remove-btn" @click="removeImage"></i>
                  </div>
                </div>
                <div v-else class="image-uploader-trigger">
                  <i class="el-icon-plus"></i>
                  <div class="upload-text">点击上传封面图片</div>
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="内容" prop="content">
          <el-input 
            v-model="instructionForm.content" 
            type="textarea" 
            :rows="10"
            placeholder="请输入指南内容"
            maxlength="2000"
            show-word-limit
            resize="vertical"
          />
        </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="instructionDialogVisible = false" icon="el-icon-close">
          取消
        </el-button>
        <el-button 
          type="primary" 
          @click="submitInstructionForm"
          :loading="submitting"
          icon="el-icon-check"
        >
          确定
        </el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog
      title="操作指南详情"
      :visible.sync="instructionDetailDialogVisible"
      width="800px"
      class="detail-dialog"
    >
      <div v-if="currentInstruction" class="instruction-detail">
        <div class="detail-image" v-if="currentInstruction.img">
          <img :src="currentInstruction.img" :alt="currentInstruction.title" />
        </div>
        <div class="detail-content-wrapper">
          <h3 class="detail-title">{{ currentInstruction.title }}</h3>
          <div class="detail-content">{{ currentInstruction.content }}</div>
          <div class="detail-meta">
            <div class="meta-item">
              <i class="el-icon-time"></i>
              <span>创建时间：{{ formatDate(currentInstruction.createdAt) }}</span>
            </div>
            <div class="meta-item">
              <i class="el-icon-refresh"></i>
              <span>更新时间：{{ formatDate(currentInstruction.updatedAt) }}</span>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import documentMixin from '@/mixins/documentMixin'

export default {
  name: 'Instructions',
  mixins: [documentMixin],
  created() {
    this.loadInstructionsList()
  }
}
</script>

<style lang="scss" scoped>
.instructions-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
  
  @media (max-width: 768px) {
    padding: 10px;
  }
}

.instructions-header {
  margin-bottom: 24px;
  
  .header-content {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(0, 0, 0, 0.05);
    
    .header-title {
      display: flex;
      align-items: center;
      margin-bottom: 8px;
      
      i {
        font-size: 24px;
        color: #409eff;
        margin-right: 12px;
      }
      
      h2 {
        margin: 0;
        font-size: 24px;
        font-weight: 600;
        color: #303133;
        letter-spacing: 0.5px;
      }
    }
    
    .header-description {
      margin: 0;
      color: #606266;
      font-size: 14px;
      line-height: 1.6;
    }
  }
}

.instructions-main {
  .action-card {
    border-radius: 12px;
    border: none;
    transition: all 0.3s ease;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    }
    
    ::v-deep .el-card__header {
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: white;
      border-radius: 12px 12px 0 0;
      padding: 16px 20px;
      border: none;
      
      .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .card-title {
          display: flex;
          align-items: center;
          
          i {
            font-size: 18px;
            margin-right: 8px;
          }
          
          span {
            font-size: 16px;
            font-weight: 600;
          }
        }
      }
    }
    
    ::v-deep .el-card__body {
      padding: 24px;
    }
  }
}

.instructions-list {
  .empty-list {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;
    text-align: center;
    
    .empty-icon {
      margin-bottom: 16px;
      
      i {
        font-size: 64px;
        color: #c0c4cc;
      }
    }
    
    .empty-text {
      font-size: 16px;
      color: #909399;
      margin: 0 0 8px 0;
      font-weight: 500;
    }
    
    .empty-hint {
      font-size: 14px;
      color: #c0c4cc;
      margin: 0;
    }
  }
  
  .instructions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 20px;
    
    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }
}

.instruction-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 1px solid rgba(0, 0, 0, 0.05);
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  }
  
  .card-image {
    position: relative;
    height: 200px;
    overflow: hidden;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s ease;
    }
    
    .no-image {
      width: 100%;
      height: 100%;
      background: #f5f7fa;
      display: flex;
      align-items: center;
      justify-content: center;
      
      i {
        font-size: 48px;
        color: #c0c4cc;
      }
    }
    
    .image-overlay {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.5);
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0;
      transition: opacity 0.3s ease;
      
      .overlay-btn {
        background: rgba(255, 255, 255, 0.9);
        color: #409eff;
        border: none;
        
        &:hover {
          background: white;
        }
      }
    }
    
    &:hover {
      img {
        transform: scale(1.05);
      }
      
      .image-overlay {
        opacity: 1;
      }
    }
  }
  
  .card-content {
    padding: 20px;
    
    .card-title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
      margin: 0 0 12px 0;
      line-height: 1.4;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
    
    .card-desc {
      font-size: 14px;
      color: #606266;
      line-height: 1.6;
      margin: 0 0 16px 0;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      line-clamp: 3;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
    
    .card-meta {
      .create-time {
        font-size: 12px;
        color: #909399;
        display: flex;
        align-items: center;
        
        i {
          margin-right: 4px;
        }
      }
    }
  }
  
  .card-actions {
    padding: 0 20px 20px;
    display: flex;
    gap: 8px;
    
    .el-button {
      flex: 1;
      padding: 8px 12px;
      border-radius: 6px;
      transition: all 0.3s ease;
      
      &:hover {
        background: #f5f7fa;
        transform: translateY(-1px);
      }
      
      &.danger-text {
        color: #f56c6c;
        
        &:hover {
          background: #fef0f0;
          color: #f56c6c;
        }
      }
    }
  }
}

// 对话框样式
.instruction-dialog {
  ::v-deep .el-dialog {
    border-radius: 12px;
    overflow: hidden;
  }
  
  ::v-deep .el-dialog__header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px 24px;
    
    .el-dialog__title {
      font-weight: 600;
    }
  }
  
  .instruction-form {
    padding: 20px 0;
    
    .image-uploader {
      .image-preview {
        position: relative;
        width: 100%;
        height: 120px;
        border-radius: 8px;
        overflow: hidden;
        
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
        
        .image-overlay {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 0.5);
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 12px;
          opacity: 0;
          transition: opacity 0.3s ease;
          
          i {
            color: white;
            font-size: 18px;
            cursor: pointer;
            padding: 8px;
            border-radius: 4px;
            background: rgba(255, 255, 255, 0.2);
            
            &:hover {
              background: rgba(255, 255, 255, 0.3);
            }
          }
        }
        
        &:hover .image-overlay {
          opacity: 1;
        }
      }
      
      .image-uploader-trigger {
        width: 100%;
        height: 120px;
        border: 2px dashed #d9d9d9;
        border-radius: 8px;
        background: #fafafa;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:hover {
          border-color: #409eff;
          background: #f0f9ff;
        }
        
        i {
          font-size: 24px;
          color: #c0c4cc;
          margin-bottom: 8px;
        }
        
        .upload-text {
          font-size: 14px;
          color: #606266;
        }
      }
    }
  }
}

.detail-dialog {
  ::v-deep .el-dialog {
    border-radius: 12px;
    overflow: hidden;
  }
  
  ::v-deep .el-dialog__header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px 24px;
  }
  
  .instruction-detail {
    .detail-image {
      width: 100%;
      height: 200px;
      border-radius: 8px;
      overflow: hidden;
      margin-bottom: 20px;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
    
    .detail-content-wrapper {
      .detail-title {
        font-size: 20px;
        font-weight: 600;
        color: #303133;
        margin: 0 0 16px 0;
        line-height: 1.4;
      }
      
      .detail-content {
        font-size: 14px;
        color: #606266;
        line-height: 1.8;
        margin-bottom: 20px;
        white-space: pre-wrap;
      }
      
      .detail-meta {
        display: flex;
        flex-direction: column;
        gap: 8px;
        
        .meta-item {
          display: flex;
          align-items: center;
          font-size: 12px;
          color: #909399;
          
          i {
            margin-right: 6px;
            font-size: 14px;
          }
        }
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .instructions-header .header-content {
    padding: 16px;
    
    .header-title h2 {
      font-size: 20px;
    }
  }
  
  .instructions-main .action-card {
    ::v-deep .el-card__body {
      padding: 16px;
    }
  }
  
  .instruction-card {
    .card-content {
      padding: 16px;
    }
    
    .card-actions {
      padding: 0 16px 16px;
      flex-direction: column;
      
      .el-button {
        flex: none;
      }
    }
  }
  
  .instruction-dialog {
    ::v-deep .el-dialog {
      width: 95% !important;
      margin: 0 auto;
    }
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.instruction-card {
  animation: fadeInUp 0.6s ease-out;
}

.instruction-card:nth-child(2) {
  animation-delay: 0.1s;
}

.instruction-card:nth-child(3) {
  animation-delay: 0.2s;
}

.instruction-card:nth-child(4) {
  animation-delay: 0.3s;
}
</style>

